<?php

namespace app\beautiful\controller\admin;

use app\BaseController;
use app\beautiful\model\mall\GoodsCategoryModel;
use app\common\model\ProduceDepartModel;
use app\common\model\ProduceModel;


class Produce extends BaseController{

    public function list(){
        $limit=$this->request->limit;
        $page=$this->request->page;
        $category_id = $this->request->param('category_id');
        $depart_id= $this->request->param('depart_id');
        $where=[];
        $shop_id=$this->request->shopId;
        $where['shop_id']=$shop_id;
        $order['a.id']='desc';
        if(!empty($category_id)){
            $category_arr=GoodsCategoryModel::where(['pid'=>$category_id,'is_delete'=>0])->select()->toArray();
            $category_ids=array_column($category_arr,'category_id');
            $category_ids[]=$category_id;
            $where['category_ids']=$category_ids;
        }
        if(!empty($depart_id)){
            $where['depart_id']=$depart_id;
        }
        $res_data=ProduceModel::list($where,$order,$page,$limit);
        return $this->success($res_data);
    }
    public function info(){
        $id = $this->request->param('id');
        if(empty($id)){
            return $this->error('ID错误');
        }
        $where['id']=$id;
        $res_data=ProduceModel::info($where);
        return $this->success($res_data);
    }
    public function del(){
        $id = $this->request->param('id');
        if(empty($id)){
            return $this->error('ID错误');
        }
        $is_delete=1;
        $where['id']=$id;
        $info=ProduceModel::info($where);
        if(empty($info)){
            return $this->error('数据不存在');
        }
        $up['is_delete']=$is_delete;
        $res=ProduceModel::up($up,$where);
        if(!$res){
            return $this->error('删除失败');
        }
        return $this->success();
    }
    public function edit(){
        $id = $this->request->param('id',0);
        $goods_id = $this->request->param('goods_id',null);
        $depart_id = $this->request->param('depart_id',null);
        $shop_id=$this->request->shopId;
        if($id==0){
            return $this->error('id错误');
        }
        $ProduceModel= new ProduceModel();
        $all_info=$ProduceModel->where(['id'=>$id])->find();
        if(empty($all_info)) return $this->error('数据不存在');
        $s=[];
        $s[]=['goods_id','=',$goods_id];
        $s[]=['shop_id','=',$shop_id];
        $s[]=['depart_id','=',$depart_id];
        $s[]=['id','<>',$id];
        $s[]=['is_delete','=',0];
        $info=$ProduceModel->where($s)->find();
        if(!empty($info)) return $this->error('数据重复');
        $up=[];
        if($goods_id!=null) $up['goods_id']=$goods_id;
        if($depart_id!=null) $up['depart_id']=$depart_id;
        $where['id']=$id;
        $res_data=ProduceModel::up($up,$where);
        return $this->success($res_data);
    }
    public function add(){
        $shop_id=$this->request->shopId;
        $goods_ids = $this->request->param('goods_ids',null);
        $depart_id=$this->request->param('depart_id',null);
        $ProduceModel= new ProduceModel();
        $goods_ids=trim($goods_ids,',');
        $goods_ids=explode(',',$goods_ids);
        if(empty($goods_ids)){
            return $this->error('产品参数错误');
        }
        foreach ($goods_ids as $goods_id){
            $s=[];
            $insert=[];
            $s[]=['goods_id','=',$goods_id];
            $s[]=['shop_id','=',$shop_id];
            $s[]=['depart_id','=',$depart_id];
            $s[]=['is_delete','=',0];
            $all_info=$ProduceModel->where($s)->find();
            if(!empty($all_info)) continue;
            $insert=compact('shop_id','goods_id','depart_id');
            $insert['add_time']=date('Y-m-d H:i:s');
            $insert['edit_time']=date('Y-m-d H:i:s');
            $insert_id=ProduceModel::insertGetId($insert);
        }
        return $this->success();
    }

}

